package com.iv.durian.cloud.api;

import com.iv.durian.cloud.*;
import com.iv.durian.cloud.api.service.LessonDetailService;
import com.iv.durian.cloud.api.service.LessonService;
import com.iv.durian.cloud.vo.LessonDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by zlw on 2018-07-01.
 */
@RestController
@RequestMapping("/api/lesson")
@Slf4j
@Api(value = "LessonDetailApi" ,description = "LessonDetailApi",tags={"lesson_detail"} )
public class LessonDetailApi {
    @Autowired
    private LessonDetailService lessonDetailService;

    @Autowired
    private LessonService lessonService;


    @ApiOperation(value = "通過id获取课程描述")
    @RequestMapping(value = "/getDetailById/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult<LessonDetailVO>> getById (
            @ApiParam(name = "lesson_id",value = "课程id",required = true) @RequestParam(value = "lesson_id") Integer lessonId ){
        JsonResult r = new JsonResult();
        try {
            LessonDetail lessonDetail = lessonDetailService.getById(lessonId);
            if (lessonDetail != null) {
                LessonDetailVO lessonDetailVO = LessonDetailVO.builder()
                        .lessonContent(lessonDetail.getLessonContent())
                        .lessonTimeLine(lessonDetail.getLessonTimeLine())
                        .build();
                r.ok(lessonDetailVO);
            }
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("通過课程id查询课程描述详情失败",e);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "通過name获取课程描述")
    @RequestMapping(value = "/getDetailByName", method = RequestMethod.GET)
    public ResponseEntity<JsonResult<LessonDetailVO>> getByName (
            @ApiParam(name = "lesson_name",value = "课程name",required = true) @RequestParam(value = "lesson_name") String lessonName ){
        JsonResult r = new JsonResult();
        try {
            LessonDetail lessonDetail = lessonDetailService.getByName(lessonName);
            if (lessonDetail != null) {
                LessonDetailVO lessonDetailVO = LessonDetailVO.builder()
                        .lessonContent(lessonDetail.getLessonContent())
                        .lessonTimeLine(lessonDetail.getLessonTimeLine())
                        .build();
                r.ok(lessonDetailVO);
            }
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("通過课程name查询课程描述详情失败",e);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "添加課程详情")
    @RequestMapping(value = "/addLessonDetail", method = RequestMethod.POST)
    public void add(
            @ApiParam(name = "lesson_id",value = "課程id",required = true) @RequestParam(value = "lesson_id") Integer lessonId,
            @ApiParam(name = "content",value = "课程描述",required = true) @RequestParam(value = "content") String content,
            @ApiParam(name = "day",value = "开课时间-天",required = true) @RequestParam(value = "day") String day,
            @ApiParam(name = "startTime",value = "开课时间-小时",required = true) @RequestParam(value = "startTime") Double startTime,
            @ApiParam(name = "endTime",value = "课程结束时间-小时",required = true) @RequestParam(value = "endTime") Double endTime){

        Lesson lesson = lessonService.getLessonById(lessonId);
        Assert.notNull(lesson,"該課程不存在，无法添加详情");

        LessonDetail findedContend = lessonDetailService.getById(lessonId);
        Assert.isNull(findedContend.getLessonContent(),"該課程描述已存在");
        Assert.isNull(findedContend.getLessonTimeLine(),"該課程时间详情已存在");

        LessonContent lessonContent = new LessonContent();
        lessonContent.setLessonId(lessonId);
        lessonContent.setContent(content);

        LessonTimeLine lessonTimeLine = new LessonTimeLine();
        lessonTimeLine.setLessonId(lessonId);

        lessonTimeLine.setDay(parseStrToDate(day));
        long etime = new Double(endTime * 3600 * 1000).longValue();
        long stime = new Double(startTime * 3600 * 1000).longValue();
        lessonTimeLine.setEndTime(new Time(etime));
        lessonTimeLine.setStartTime(new Time(stime));

        LessonDetail detailBean = new LessonDetail();
        detailBean.setLessonContent(lessonContent);
        detailBean.setLessonTimeLine(lessonTimeLine);
        lessonDetailService.add(detailBean);
    }

    @ApiOperation(value = "刪除某課程对应的详情byId")
    @RequestMapping(value = "/removeLessonDetailById", method = RequestMethod.POST)
    public void removeLessonById(
            @ApiParam(name = "lesson_id",value = "课程id",required = true) @RequestParam(value = "lesson_id") Integer lessonId){
        lessonDetailService.removeById(lessonId);
    }

    @ApiOperation(value = "更新課程详情")
    @RequestMapping(value = "/changeLessonDetail", method = RequestMethod.POST)
    public void changeLessonDetail(
            @ApiParam(name = "lesson_detail",value = "課程详情",required = true) @RequestBody LessonDetail lessonDetail){
        lessonDetailService.updateLessonDetail(lessonDetail);
    }

    @ApiOperation(value = "查詢課程详情總數")
    @RequestMapping(value = "/countLessonDetail", method = RequestMethod.GET)
    public int getCount(
            @ApiParam(name = "lesson_id",value = "课程id",required = true) @RequestParam(value = "lesson_id") Integer lessonId){
        return lessonDetailService.countLessonDetail(lessonId);
    }

    /**
     * 将形如"2017-05-25"的字符串转化为Date
     * @param dateStr
     * @return
     */
    private Date parseStrToDate (String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(dateStr);
        } catch (ParseException e) {
            log.error("[日期转化异常: {}]", e);
        }
        return date;
    }
}
